package com.yanqu.road.dao.impl.cross.mammon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.decoration.MammonRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossAreaMammonRecordDaoImpl extends TempDataObjectDao<MammonRecord> {

    public void getCrossMammonRecordMap(Map<Long, Map<String, MammonRecord>> allCrossMammonRecordMap, Map<Integer, Map<String, MammonRecord>> crossGroupMammonRecordMap) {
        String sql = "select * from t_c_area_mammon_record order by recordTime";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                MammonRecord data = parse(rs);
                Map<String, MammonRecord> tempServerMap = allCrossMammonRecordMap.get(data.getUserBaseInfo().getServerId());
                if (null == tempServerMap) {
                    tempServerMap = new ConcurrentHashMap<>();
                    allCrossMammonRecordMap.put(data.getUserBaseInfo().getServerId(), tempServerMap);
                }
                tempServerMap.put(data.getRecordUid(), data);

                if (!crossGroupMammonRecordMap.containsKey(data.getType())) {
                    crossGroupMammonRecordMap.put(data.getType(), new ConcurrentHashMap<>());
                }
                crossGroupMammonRecordMap.get(data.getType()).put(data.getRecordUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    @Override
    protected boolean addData(MammonRecord data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_c_area_mammon_record(recordUid,type,userId,userBaseInfo,recordTime) VALUES(?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getRecordUid());
            call.setInt(i++, data.getType());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            call.setLong(i++, data.getRecordTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(MammonRecord data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_c_area_mammon_record SET type=?,userId=?,userBaseInfo=?,recordTime=? where recordUid=?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getType());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            call.setLong(i++, data.getRecordTime());
            call.setString(i++, data.getRecordUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected MammonRecord parseData(ResultSet rs) throws SQLException {
        MammonRecord data = new MammonRecord();
        data.setRecordUid(rs.getString("recordUid"));
        data.setType(rs.getInt("type"));
        data.setUserId(rs.getLong("userId"));
        data.setRecordTime(rs.getLong("recordTime"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if (null == baseUserInfoStr) {
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {
        });
        if (null != userBaseInfo) {
            data.setUserBaseInfo(userBaseInfo);
        }
        return data;
    }
}
